export function setCookie(key, value, options = {}) {
  const defaultOptions = { expires: 7, path: "/" };
  options = { ...defaultOptions, ...options };

  if (typeof options.expires === "number") {
    const date = new Date();
    date.setTime(date.getTime() + options.expires * 24 * 60 * 60 * 1000);
    options.expires = date.toUTCString();
  }
  let updatedCookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
  for (const optionKey in options) {
    updatedCookie += `; ${optionKey}`;
    const optionValue = options[optionKey];
    if (optionValue !== true) {
      updatedCookie += `=${optionValue}`;
    }
  }
  document.cookie = updatedCookie;
}
export function getCookie(key) {
  const cookies = document.cookie.split("; ");
  for (let i = 0; i < cookies.length; i++) {
    const cookie = cookies[i].split("=");
    if (cookie[0] === key) {
      return decodeURIComponent(cookie[1]);
    }
  }
  return null;
}
export function deleteCookie(key) {
  setCookie(key, "", { expires: -1 });
}

export function clearAllCookies() {
  const cookies = document.cookie.split("; ");
  for (let i = 0; i < cookies.length; i++) {
    const cookie = cookies[i].split("=")[0];
    deleteCookie(cookie);
  }
}
export function getAllCookies() {
  const cookies = document.cookie.split("; ");
  const cookieObj = {};
  for (let i = 0; i < cookies.length; i++) {
    const cookie = cookies[i].split("=");
    cookieObj[cookie[0]] = decodeURIComponent(cookie[1]);
  }
  return cookieObj;
}
